import datetime
import wsgiref.handlers
import logging
log = logging.getLogger("dbmaint")

from google.appengine.ext import webapp
from google.appengine.ext import db

import statsdb

class UpdateTag(webapp.RequestHandler):
  def get(self):
    if 0 == len(self.request.get('begin')):
      begin = datetime.datetime.strptime("197001010000", "%Y%m%d%H%M%S")
    else:
      begin = datetime.datetime.strptime(self.request.get('begin'), "%Y%m%d%H%M%S")
    if 0 == len(self.request.get('end')):
      end = datetime.datetime.strptime("207001010000", "%Y%m%d%H%M%S")
    else:
      end = datetime.datetime.strptime(self.request.get('end'), "%Y%m%d%H%M%S")
    self.response.headers['Content-Type'] = 'text/plain'
    
    if len(self.request.get('pagesize')) > 0:
      pagesize = int(self.request.get('pagesize'))
    else:
      pagesize = 100
    tagsName = []
    bookmark = None
    while True:
      records, bookmark = statsdb.GetRecords("inputtime", begin, end, [], bookmark, pagesize)
      for record in records:
        tagsName = tagsName + list(set(record.tags)-set(tagsName))
      self.response.out.write(str(len(records)) + " records counted.\n")
      if bookmark is None: break
      
    tags = {}
    for tagName in tagsName:
      tag = statsdb.Tag(key_name = statsdb.GenTagKey(tagName), name = tagName)
      bookmark = None
      begin = datetime.datetime.strptime("197001010000", "%Y%m%d%H%M%S")
      end = datetime.datetime.strptime("207001010000", "%Y%m%d%H%M%S")
      while True:
        records, bookmark = statsdb.GetRecords("date", begin, end, [tagName], bookmark)
        tag.refCount += len(records)
        if bookmark is None: break
      tags[statsdb.GenTagKey(tagName)] = tag
    if len(self.request.get('batch')) > 0:
      batch = int(self.request.get('batch'))
    else:
      batch = 50
    statsdb.DBPut(tags.values(), batch)
    self.response.out.write(str(len(tags)) + " tags counted.\n")
      
class UpdateHistory(webapp.RequestHandler):
  def get(self):
    if 0 == len(self.request.get('begin')):
      begin = datetime.datetime.strptime("197001010000", "%Y%m%d%H%M%S")
    else:
      begin = datetime.datetime.strptime(self.request.get('begin'), "%Y%m%d%H%M%S")
    if 0 == len(self.request.get('end')):
      end = datetime.datetime.strptime("207001010000", "%Y%m%d%H%M%S")
    else:
      end = datetime.datetime.strptime(self.request.get('end'), "%Y%m%d%H%M%S")
    self.response.headers['Content-Type'] = 'text/plain'
    
    if len(self.request.get('pagesize')) > 0:
      pagesize = int(self.request.get('pagesize'))
    else:
      pagesize = 100
    histories = {}
    bookmark = None
    while True:
      records, bookmark = statsdb.GetRecords("inputtime", begin, end, [], bookmark, pagesize)
      for record in records:
        if True == statsdb.GetHistory(record.tags)[0] \
        or True == histories.has_key(statsdb.GenHistoryKey(record.tags)):
          continue
        histories[statsdb.GenHistoryKey(record.tags)] = \
          statsdb.History(key_name=statsdb.GenHistoryKey(record.tags),
                          tags=record.tags)      
      self.response.out.write(str(len(records)) + " records counted.\n")
      if bookmark is None: break

    if len(self.request.get('batch')) > 0:
      batch = int(self.request.get('batch'))
    else:
      batch = 50
    statsdb.DBPut(histories.values(), batch)
    statsdb.UpdateEntity(histories, True)
    self.response.out.write(str(len(histories)) + " histories counted.\n")
    
class DeleteRecord(webapp.RequestHandler):
  def get(self):
    query = statsdb.Record.all()
    self.response.headers['Content-Type'] = 'text/plain'
    if query.count() <= 0:
      self.response.out.write("No more uncounted records.\n")
      return
    
    if len(self.request.get('batch')) > 0:
      batch = int(self.request.get('batch'))
    else:
      batch = 100
    while True:
      records = query.fetch(batch)
      if 0 == len(records): break
      db.delete(records)
      self.response.out.write(str(len(records)) + " records deleted.\n")
      statsdb.UpdateEntity(dict((statsdb.GenRecordKey(k.date, k.tags), None) for k in records), False)
    

class ViewRecord(webapp.RequestHandler):
  def get(self):
    if 0 == len(self.request.get('begin')):
      begin = datetime.datetime.strptime("197001010000", "%Y%m%d%H%M%S")
    else:
      begin = datetime.datetime.strptime(self.request.get('begin'), "%Y%m%d%H%M%S")
    if 0 == len(self.request.get('end')):
      end = datetime.datetime.strptime("207001010000", "%Y%m%d%H%M%S")
    else:
      end = datetime.datetime.strptime(self.request.get('end'), "%Y%m%d%H%M%S")
    if 0 == len(self.request.get('tags')):
      tags = []
    else:
      tags = self.request.get('tags').split(" ")
    if 0 == len(self.request.get('op')):
      op = "count"
    else:
      op = self.request.get('op')
    if len(self.request.get('pagesize')) > 0:
      pagesize = int(self.request.get('pagesize'))
    else:
      pagesize = 100
    bookmark = None
    count = 0
    while True:
      records, bookmark = statsdb.GetRecords("date", begin, end, tags, bookmark, pagesize)
      count += len(records)
      if "print" == op:
        for r in records:
          print r.date.strftime(str(r.dfmt)), r.value, r.unit, " ".join(r.tags)
      if bookmark is None: break
    self.response.out.write(str(count) + " records counted\n")

def main():
  application = webapp.WSGIApplication([('/dbupdatetag', UpdateTag), 
                                        ('/dbupdatehistory', UpdateHistory),
                                        ('/dbdeleterecord', DeleteRecord),
                                        ('/dbviewrecord', ViewRecord),
                                        ],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
